public class LinkedList implements ILinkedList {

    //结点类
    static class ListNode {
        public ListNode prv;//前驱
        public ListNode next;//后继
        int val;

        public ListNode(int val) {
            this.val = val;
        }
    }

    //头尾结点
    public ListNode head;
    public ListNode tail;


    public void addFirst(int data) {
        ListNode newNode = new ListNode(data);
        if(head == null) {
            head = newNode;
            tail = newNode;
            return;
        }

        head.prv = newNode;
        newNode.next = head;
        head = newNode;

    }

    public void addLast(int data) {
        ListNode newNode = new ListNode(data);
        if(head == null) {
            head = newNode;
            tail = newNode;
            return;
        }

        tail.next = newNode;
        newNode.prv = tail;
        tail = newNode;
    }

    public void addIndext(int pos, int data) {

        if(pos < 0 || pos > size()) {
            System.out.println("下标位置不合法！！！");
            return;
        }

        //头插
        if(pos == 0) {
            addFirst(data);
            return;
        }

        //尾插
        if(pos == size()) {
            addLast(data);
            return;
        }


        ListNode cur = head;
        ListNode newNode = new ListNode(data);
        for(int i = 0; i < pos; i++) {
            cur = cur.next;
        }

        //出了循环，说明cur在下标pos结点
        newNode.prv = cur.prv;
        newNode.next = cur;
        cur.prv.next = newNode;
        cur.prv = newNode;

    }

    public boolean contains(int data) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == data) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public void remove(int key) {
        ListNode cur = head;

        //如果头结点就是要删除的结点
        if(head.val == key) {
            head = head.next;
            //如果链表只有头结点
            if(head == null) {
                tail = null;
            } else {
                head.prv = null;
            }
            return;
        }


        while(cur != null) {
            if(cur.val == key) {

                //判断是不是tail
                if(cur.next == null) {
                    tail = tail.prv;
                    tail.next = null;
                    return;
                }
                cur.next.prv = cur.prv;
                cur.prv.next = cur.next;
                return;
            }
            cur = cur.next;
        }

    }

    public void removeAllKey(int key) {
        ListNode cur = head;

        //如果头结点就是要删除的结点
        if(head.val == key) {
            head = head.next;
            //如果链表只有头结点
            if(head == null) {
                tail = null;
            } else {
                head.prv = null;
            }
            return;
        }


        while(cur != null) {
            if(cur.val == key) {

                //判断是不是tail
                if(cur.next == null) {
                    tail = tail.prv;
                    tail.next = null;
                    return;
                }
                cur.next.prv = cur.prv;
                cur.prv.next = cur.next;
            }
            cur = cur.next;
        }
    }


    public int size() {
        ListNode cur = head;
        int count = 0;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }


    public void show() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val + "->");
            cur = cur.next;
        }
        System.out.println();
    }

    public void clear() {
        ListNode cur = head;
        ListNode curN = cur.next;
        while(cur.next != null) {
            cur.prv = null;
            cur.next = null;
            cur = curN;
            curN = curN.next;
        }

        //这俩个不能少，因为只是head和tail的next和prv为null，但是head和tail的结点还是存在
        head = null;
        tail = null;
    }
}
